# encoding: utf-8
# 登录模块
# @Author: dpf
# @Date  : 2024/11/27/21:00
import sys

from PyQt6.QtWidgets import QWidget, QApplication, QMessageBox
from PyQt6 import QtCore, QtGui, QtWidgets

import main
from dao import userDao
from entity.UserModel import User


class Ui_Form(QWidget):

    def __init__(self):
        super(Ui_Form, self).__init__()
        self.setupUi(self)

    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(537, 372)
        self.formLayoutWidget = QtWidgets.QWidget(parent=Form)
        self.formLayoutWidget.setGeometry(QtCore.QRect(130, 110, 271, 121))
        self.formLayoutWidget.setObjectName("formLayoutWidget")
        self.formLayout = QtWidgets.QFormLayout(self.formLayoutWidget)
        self.formLayout.setContentsMargins(10, 10, 10, 10)
        self.formLayout.setHorizontalSpacing(20)
        self.formLayout.setVerticalSpacing(40)
        self.formLayout.setObjectName("formLayout")
        self.label = QtWidgets.QLabel(parent=self.formLayoutWidget)
        self.label.setObjectName("label")
        self.formLayout.setWidget(0, QtWidgets.QFormLayout.ItemRole.LabelRole, self.label)
        self.logNameInput = QtWidgets.QLineEdit(parent=self.formLayoutWidget)
        self.logNameInput.setObjectName("logNameInput")
        self.formLayout.setWidget(0, QtWidgets.QFormLayout.ItemRole.FieldRole, self.logNameInput)
        self.label_2 = QtWidgets.QLabel(parent=self.formLayoutWidget)
        self.label_2.setObjectName("label_2")
        self.formLayout.setWidget(1, QtWidgets.QFormLayout.ItemRole.LabelRole, self.label_2)
        self.passwordInput = QtWidgets.QLineEdit(parent=self.formLayoutWidget)
        self.passwordInput.setEchoMode(QtWidgets.QLineEdit.EchoMode.Password)
        self.passwordInput.setObjectName("passwordInput")
        self.formLayout.setWidget(1, QtWidgets.QFormLayout.ItemRole.FieldRole, self.passwordInput)
        self.loginBtn = QtWidgets.QPushButton(parent=Form)
        self.loginBtn.setGeometry(QtCore.QRect(150, 270, 75, 30))
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("./images/login_btn.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
        self.loginBtn.setIcon(icon)
        self.loginBtn.setIconSize(QtCore.QSize(24, 24))
        self.loginBtn.setObjectName("loginBtn")
        # 登录按钮绑定点击槽函数
        self.loginBtn.clicked.connect(self.login)
        self.resetBtn = QtWidgets.QPushButton(parent=Form)
        self.resetBtn.setGeometry(QtCore.QRect(290, 270, 75, 30))
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap("./images/reset_btn.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
        self.resetBtn.setIcon(icon1)
        self.resetBtn.setIconSize(QtCore.QSize(18, 18))
        self.resetBtn.setObjectName("resetBtn")
        # 重置按钮绑定点击槽函数
        self.resetBtn.clicked.connect(self.resetForm)
        self.label_3 = QtWidgets.QLabel(parent=Form)
        self.label_3.setGeometry(QtCore.QRect(210, 50, 151, 31))
        font = QtGui.QFont()
        font.setPointSize(17)
        font.setBold(True)
        font.setItalic(False)
        font.setUnderline(False)
        font.setStrikeOut(False)
        self.label_3.setFont(font)
        self.label_3.setFrameShape(QtWidgets.QFrame.Shape.Box)
        self.label_3.setLineWidth(0)
        self.label_3.setObjectName("label_3")
        self.label_4 = QtWidgets.QLabel(parent=Form)
        self.label_4.setGeometry(QtCore.QRect(120, 30, 61, 61))
        self.label_4.setText("")
        self.label_4.setPixmap(QtGui.QPixmap("./images/logo.png"))
        self.label_4.setObjectName("label_4")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "图书管理系统"))
        self.label.setText(_translate("Form", "用户名"))
        self.label_2.setText(_translate("Form", "密码"))
        self.loginBtn.setText(_translate("Form", "登录"))
        self.resetBtn.setText(_translate("Form", "重置"))
        self.label_3.setText(_translate("Form", "图书管理系统"))

    # 重置表单的方法
    def resetForm(self):
        self.logNameInput.setText("")
        self.passwordInput.setText("")

    # 用户登陆
    def login(self):
        logName = self.logNameInput.text()
        password = self.passwordInput.text()
        if logName.strip() == "" or password.strip() == "":
            QMessageBox.warning(None, '登录提示', '用户名或密码不能为空')
        else:
            user = User(logName, password)
            resultUser = userDao.login(user)
            if resultUser:
                print("登录成功")
                userDao.currentUser = user
                self.m = main.Ui_MainWindow()
                self.m.show()
                self.hide()
            else:
                QMessageBox.warning(None, '登录提示', '用户名或密码错误')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ui = Ui_Form()
    ui.show()
    sys.exit(app.exec())
